const dbasync=require("./sqlit3_async.js")
console.log(dbasync)
let filename=__dirname+"/key.sqlite"
async function selectPassword(service, account) {
    let cmd=`select * from pwd where service='${service}' and account='${account}'` ;
    console.log(cmd);
    return await dbasync.get(cmd)
}
async function setPassword(service, account,password) {
    let r=await selectPassword(service,account)
    if(r){//find
        await updatePassword(service, account,password)
    }else{
        await createPassword(service, account,password)
    }
}
async function createPassword(service, account,password) {
    let cmd=`insert into pwd(service,account,password) values('${service}','${account}','${password}')` ;
    await dbasync.run(cmd);
}
async function updatePassword(service, account,password) {
    let cmd=`update pwd set password='${password}' where service='${service}' and account='${account}'` ;
    await dbasync.run(cmd);
}
async function deletePassword(service, account) {
    let cmd=`delete from  pwd where service='${service}' and account='${account}'` ;
    await dbasync.run(cmd);
}
async function createTable() {
    let cmd="CREATE TABLE pwd (service TEXT, account TEXT, password TEXT, PRIMARY KEY (service, account))"
    await dbasync.run(cmd);
}
async function selectAccount(service,callback) {
    let cmd=`select * from pwd where service='${service}'`
    console.log(cmd);
    return await dbasync.all(cmd)
}
async function main(argument) {
    // body...
    console.log(await dbasync.open(filename))
    // console.log(await selectPassword('localhost','mahongquan'))
    await setPassword('localhost','mahongquan','111111')
    let a=await selectAccount('localhost');
    console.log(a);
}
// main()
exports={selectAccount,selectPassword,createTable,setPassword}